﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Common;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Modules;

namespace Web.Cms.Member
{
    public partial class MemberManager : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private int cPage = 1;
        private DateTime dateTo;
        private DateTime dateFrom;
        #endregion

        #region lấy giá trị khi request
        void GetRequest()
        {
            if (Request.QueryString["keyword"] != null || Request.QueryString["keyword"] != "")
                txtKeysearch.Text = Request.QueryString["keyword"];
           

            if (Request.QueryString["dateFrom"] != null && Request.QueryString["dateFrom"] != "")
            {
                dateFrom = DateTime.Parse(Request.QueryString["dateFrom"], CultureInfo.CreateSpecificCulture("vi-VN"));
                txtDatefrom.Text = dateFrom.ToString("dd/MM/yyyy");
            }
            else
            {
                dateFrom = DateTime.Now.AddMonths(-2);
                txtDatefrom.Text = dateFrom.ToString("dd/MM/yyyy");
            }

            if (Request.QueryString["dateTo"] != null && Request.QueryString["dateTo"] != "")
            {
                dateTo = DateTime.Parse(Request.QueryString["dateTo"], CultureInfo.CreateSpecificCulture("vi-VN"));
                txtDateTo.Text = dateTo.ToString("dd/MM/yyyy");
            }
            else
            {
                dateTo = DateTime.Now;
                txtDateTo.Text = dateTo.ToString("dd/MM/yyyy");
            }
           

        }
        #endregion

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //Fill dữ liệu CMS_ClubName
           
            GetRequest();
        }
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        #region LoadData
        private void LoadData()
        {
           
                if (Request.QueryString.Get("cpage") != null)
                    cPage = ConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
                int from = (cPage - 1)*ObjConstants.pageSize20 + 1;
                if (from == 0) from = 1;
                int to = (cPage)*ObjConstants.pageSize20;

                totalRecords = MemberBL.GetCount(txtKeysearch.Text, active, dateFrom, dateTo.AddDays(+1), from, to);

                Paging1.totalRecords = totalRecords;
                Paging1.numPerPage = ObjConstants.pageSize20;

                Paging1.queryParam = MiscUtility.GetCPPage("ListMemberActive",new string[]
                                                  {"keyword", "from", "to", "dateFrom", "dateTo", "cPage"},
                               new string[] { txtKeysearch.Text, from.ToString(), to.ToString(), txtDatefrom.Text, txtDateTo.Text, "" });
                DataTable dt;
                string order = null;
                dt = MemberBL.Search(txtKeysearch.Text, active, dateFrom, dateTo.AddDays(+1), from, to, order);
                if (dt != null && dt.Rows.Count > 0)
                {
                    grdData.DataSource = dt;
                    grdData.RowDataBound += new GridViewRowEventHandler(grdData_RowDataBound);
                    grdData.DataBind();
                }
                else
                {
                    grdData.Visible = false;
                    if (Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "")
                        lblMessage.Text = "Không tìm thấy dữ liệu";
                    else
                        lblMessage.Text = "Dữ liệu đang được cập nhật";
                }
          
                

        }
        #endregion

        #region event RowCommand
        protected void grdData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = ConvertUtility.ToInt32(e.CommandArgument);
                switch (e.CommandName)
                {
                   case "Commanddelete":

                        MemberBL.Delete(id);// xóa video

                        // Insert Cores_Audits
                        ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                        AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Delete, CurrentCommand.CtrlKey, CurrentUser.UserID, id
                                                    , "Xóa thành viên Id:" + id, 1, DateTime.Now);
                        //end                      
                        break;

                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region RowDataBound
        void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton btnStatus = (ImageButton)e.Row.FindControl("btnStatus");
                e.Row.Cells[0].Text = (ObjConstants.pageSize20 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
                

            }
        }
        #endregion

        #region tìm kiếm
        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            if(active==1)
            {
                Response.Redirect("/Cms/Default.aspx?ctrlkey=ListMemberActive&keyword=" + txtKeysearch.Text.Trim()  + "&dateFrom=" + txtDatefrom.Text.Trim().ToString() + "&dateTo=" + txtDateTo.Text.Trim());
            }
            else
            {
                Response.Redirect("/Cms/Default.aspx?ctrlkey=ListMemberUnActive&keyword=" + txtKeysearch.Text.Trim() + "&dateFrom=" + txtDatefrom.Text.Trim().ToString() + "&dateTo=" + txtDateTo.Text.Trim());
            }
           
        }
        #endregion

        #region Active
        private int active;
        public int Active
        {
            set { active = value; }
            get { return active; }
        }
        #endregion
    }
}